﻿using MCPlatform.MotionLib.Base.AssitantClass;
using MCPlatform.MotionLib.Base.Environment;
using Newtonsoft.Json;
using Panuon.WPF;
using Panuon.WPF.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace MCPlatform.MotionLib.Base.Models
{
    public class IOParaModel : NotifyPropertyChangedBase, INameProvider
    {
        [JsonIgnore]
        public IIO? IOEntity { get => _IOEntity; set => Set(ref _IOEntity, value); }

        [JsonIgnore]
        private IIO? _IOEntity;
        public string ControlName
        {
            get => _ControlName;
            set
            {
                Set(ref _ControlName, value);

            }
        }
        private string _ControlName = string.Empty;

        public int DelayTime
        {
            get => _DelayTime;
            set => Set(ref _DelayTime, value);
        }
        private int _DelayTime = 10000;


        public bool IsShield
        {
            get => _IsShield;
            set => Set(ref _IsShield, value);
        }
        private bool _IsShield;


        [JsonIgnore]
        public ICommand? ApplyCommand
        {
            get => _ApplyCommand;
            set => Set(ref _ApplyCommand, value);
        }
        [JsonIgnore]
        private ICommand? _ApplyCommand;

        [JsonIgnore]
        public ICommand? ApplyAndSaveCommand
        {
            get => _ApplyAndSaveCommand;
            set => Set(ref _ApplyAndSaveCommand, value);
        }
        [JsonIgnore]
        private ICommand? _ApplyAndSaveCommand;


        public IOParaModel()
        {
            ApplyCommand = new Command(Apply);

            ApplyAndSaveCommand = new Command(ApplyAndSave);

            ControlName = IOEntity is null ? string.Empty : IOEntity.IOName;
        }


        /// <summary>
        /// 应用
        /// </summary>
        /// <param name="O"></param>
        public void Apply(Object O)
        {
            if (IOEntity is null)
            {
                return;
            }

            IOEntity.IsShield = IsShield;

            IOEntity.DelayTime = DelayTime;

            Toast.Show("应用成功!", targetWindow: ToastWindow.MainWindow);
        }

        /// <summary>
        /// 应用并保存
        /// </summary>
        /// <param name="O"></param>
        public void ApplyAndSave(Object O)
        {
            if (IOEntity is null)
            {
                return;
            }

            ControlName = IOEntity.IOName;

            IOEntity.IsShield = IsShield;

            IOEntity.DelayTime = DelayTime;

            String path = Paths.IOConfigPaths;

            SaveConfigData.SaveConfig<IOParaModel>(path, this);

        }

        public void MappingFromEntity()
        {
            if (IOEntity is null)
            {
                return;
            }
            IOEntity.IOConfig.ControlName = IOEntity.IOName;
            //  Common.AutoMapPropertiesNotInterface(MotionEntity, this);

            Common.AutoMapPropertiesNotInterface(IOEntity.IOConfig, this);
        }
    }
}
